package file_utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

import utils.ArgumentAssertion;

public class FileIterator implements Iterator<String> {

	private BufferedReader reader;
	private String nextLine;
	
	public FileIterator(File file) throws IOException {
		ArgumentAssertion.assertNotNull("file", file);
		
		this.reader = new BufferedReader(new FileReader(file));
		this.nextLine = reader.readLine();
	}
	
	@Override
    public boolean hasNext() {
	    return nextLine != null;
    }

	@Override
    public String next() {
	    String currentLine = nextLine;
	    
	    try {
	        nextLine = reader.readLine();
        } catch (IOException e) {
	       throw new RuntimeException(e);
        }
	    
	    return currentLine;
    }

	@Override
    public void remove() {
	    throw new UnsupportedOperationException("Remove is not supported for file iterator.");
    }
}
